package Java8实战._1_Lambda._6_使用匿名类;

import Java8实战._1_Lambda.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _6_使用匿名类 {

    public static void main(String[] args) {
        List<Apple> inventory = Arrays.asList(
                new Apple("red",150),
                new Apple("green",123),
                new Apple("green",189)
        );

        List<Apple> result;

        result = filterApples(inventory, new ApplePredicate() {
            @Override
            public boolean test(Apple apple) {
                return "green".equals(apple.getColor());
            }
        });

        printApples(result, new AppleFormatter() {
            @Override
            public String accept(Apple apple) {
                return "苹果的颜色：" + apple.getColor() + "，苹果的重量：" + apple.getWeight();
            }
        });

    }

    public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate applePredicate){

        List<Apple> list = new ArrayList<>();
        for (Apple apple : inventory){
            if (applePredicate.test(apple)){
                list.add(apple);
            }
        }
        return list;
    }

    public static void printApples(List<Apple> inventory, AppleFormatter appleFormatter){

        for (Apple apple : inventory){
            System.out.println(appleFormatter.accept(apple));
        }
    }
}
